
<%@page import="org.apache.tools.ant.types.CommandlineJava.SysProperties"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8" session="false" %>	
<%@page errorPage="error.jsp" %>
<%@page import="java.io.File"%>
<%@page import="java.util.*"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%>
<%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%>
<%@page import="org.apache.commons.fileupload.FileItem"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="com.eos.data.datacontext.DataContextManager"%>
<%@page import="com.eos.data.datacontext.IMUODataContext"%>
<%@page import="com.eos.data.datacontext.IUserObject"%>
<%@page import="com.eos.foundation.data.DataObjectUtil"%>
<%@page import="commonj.sdo.DataObject"%>
<%@page import="com.eos.engine.component.ILogicComponent"%>
<%@page import="com.primeton.ext.engine.component.LogicComponentFactory"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.PrintWriter"%>	
<%@page import="java.net.URLEncoder"%>
<%
	String resultStr = "";
	String BenLuJing = "attachment\\" + "Upload\\";
	boolean isFileUpload = ServletFileUpload.isMultipartContent(request);
	 System.out.print(isFileUpload);
	if(isFileUpload==false){
	  resultStr = "请选择上传文件!!!";
	}
	if(isFileUpload){
		DiskFileItemFactory GongChang = new DiskFileItemFactory();
		ServletFileUpload fileUpload = new ServletFileUpload(GongChang); 
		fileUpload.setHeaderEncoding("UTF-8");
		List<FileItem> fileItemList = fileUpload.parseRequest(request);
		// 服务器根目录
		String rootPath = request.getRealPath("\\");
		System.out.print("333333333333"+rootPath);
		
		String uploadDir = rootPath + "\\uploads\\";
		File dirFile = new File(uploadDir);
		if (!dirFile.exists() && !dirFile.isDirectory()) {
			dirFile.mkdirs();
		}

		// 当前用户信息
		String userName = "";
		IMUODataContext muo = DataContextManager.current().getMUODataContext();
        if (muo != null) {
            IUserObject userobject = muo.getUserObject();
            if (userobject != null) {
                userName = userobject.getUserName();
            }
        }	
      
		// 处理文件信息
		for(FileItem item: fileItemList){
			if (!item.isFormField()) {
				String fileName = item.getName();
				String fileExt = fileName.substring(fileName.lastIndexOf(".")).toLowerCase();
				
				String filename1=fileName.substring(0, fileName.indexOf("."));
				// 获取文件唯一名称
				String fileUID = UUID.randomUUID().toString();
				String newFileName = "\\uploads\\"+fileUID+"."+filename1+fileExt;
				// 创建指定路径的文件
				File newFile = new File(rootPath + newFileName);
				// 处理文件信息的保存	
				try {					   		 					  			 
					resultStr = "上传成功!!!";
					//String value=fileName+","+newFileName;
			        Cookie c=new Cookie("address", URLEncoder.encode(newFileName,"utf-8"));
					c.setMaxAge(30);
					response.addCookie(c);;
				} catch(Throwable e) {
					//System.out.println(e);
					resultStr = "上传失败!!!"+e;
				}
				//out.println(resultStr);
				item.write(newFile);
			}
		}
	}
	
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
 	<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
</head>
<body>
	<script src="<%= request.getContextPath() %>/common/nui/nui.js" type="text/javascript"></script>
    <script type="text/javascript">
		nui.alert('<%=resultStr %>', '',  
		function(action){
			<% if (isFileUpload) {%>
            if (window.CloseOwnerWindow)
            return window.CloseOwnerWindow(action);
            else window.close();
            <% } else { %>
            history.go(-1)
            <% } %>
        });
    </script>
 </body>
</html>